var table = null;
var form = null;
var layer = null;
$(function () {
    forum.initLayui();
});
var forum = {
    /**
     * 初始化layui
     */
    initLayui: () => {
        layui.use(['table', 'form', 'layer'], function () {
            table = layui.table;
            form = layui.form;
            layer = layui.layer;
            // 加载表格
            forum.queryForumData();
            forum.toolbarListener();
            forum.barListener();
        });
    },
    /**
     * 监听按钮点击
     */
    toolbarListener: () => {
        table.on('toolbar(forumTableFilter)', function (obj) {
            let checkStatus = table.checkStatus(obj.config.id);
            //获取选中的数据
            let data = checkStatus.data;
            switch (obj.event) {
                // 新增
                case 'add':
                    layer.open({
                        id: 'addUserDialog',
                        type: 1,
                        title: ['新增'],
                        skin: 'layui-layer-lan',
                        area: '500px',
                        offset: 'auto',
                        content: $('#forumDialog'),
                        btn: ['提交', '取消'],
                        success: (layero, index) => {
                            // 让按钮居中显示
                            layero.find('.layui-layer-btn').css('text-align', 'center');
                        },
                        end: function () {
                            $("#forumForm")[0].reset();
                            $('#forumDialog').hide();
                        },
                        yes: function (index, layero) {
                            let data = form.val('forumForm');
                            // 向后台发送数据
                            jsonPost(data, '/forum/addForum', res => {
                                // 判断提交成功还是失败
                                if (res && res > 0) {
                                    layer.msg('新增成功', {icon: 1, time: 1500}, () => {
                                        $("#forumForm")[0].reset();
                                        form.render();
                                        // 关闭所有弹窗
                                        layer.close(index);
                                        // 刷新表格数据
                                        table.reload('forumTableId');
                                    })
                                } else {
                                    layer.msg('新增失败', {icon: 5, time: 1500}, () => {
                                        table.reload('forumList');
                                    })
                                }
                            }, error => {
                                console.error(error)
                                layer.msg('服务器内部异常', {icon: 5, time: 1500})
                            })
                        },
                        btn2: function (index, layero) {
                            layer.close(index)
                        }

                    })
                    break;
                // 默认
                default:
                    console.log('无对应按钮事件')
                    break;
            }
        })
    },
    /**
     * 行点击监听
     */
    barListener: () => {
        //todo 监听行单击事件
        table.on('tool(forumTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'update') {
                layer.open({
                    id: 'update',
                    type: 1,
                    title: ['修改'],
                    skin: 'layui-layer-lan',
                    area: '500px',
                    offset: 'auto',
                    content: $('#forumDialog'),
                    btn: ['提交', '取消'],
                    success: (layero, index) => {
                        // 让按钮居中显示
                        layero.find('.layui-layer-btn').css('text-align', 'center');
                        form.val("forumForm", {
                            "title": data.title,
                            "content": data.content
                        });
                    },
                    end: function () {
                        $("#forumForm")[0].reset();
                        $('#forumDialog').hide();
                    },
                    yes: function (index, layero) {
                        let _req = form.val('forumForm');
                        _req.id = data.id;
                        // 向后台发送数据
                        jsonPost(_req, '/forum/toUpdateForum', res => {
                            // 判断提交成功还是失败
                            if (res && res > 0) {
                                layer.msg('修改成功', {icon: 1, time: 1500}, () => {
                                    $("#forumForm")[0].reset();
                                    form.render();
                                    // 关闭所有弹窗
                                    layer.close(index);
                                    // 刷新表格数据
                                    table.reload('forumTableId');
                                })
                            } else {
                                layer.msg('修改失败', {icon: 5, time: 1500});
                            }
                        }, error => {
                            console.error(error)
                            layer.msg('服务器内部异常', {icon: 5, time: 1500})
                        })
                    },
                    btn2: function (index, layero) {
                        layer.close(index)
                    }
                })
            } else if (obj.event === 'delete') {
                layer.confirm('是否删除？', {title: '提示'}, function (index) {
                    get({id: data.id}, '/forum/deleteForum', res => {
                        if (res && res > 0) {
                            layer.msg("删除成功", {icon: 1, time: 1500}, () => {
                                // 重新刷新表格
                                table.reload('forumTableId');
                            })
                        } else {
                            layer.msg("删除失败", {icon: 5, time: 1500})
                        }
                    }, error => {
                        console.error(error)
                        layer.msg('服务器内部异常', {icon: 5, time: 1500})
                    })
                });
            }
        });
    },
    /**
     * 查询用户列表
     */
    queryForumData: () => {
        let _req = form.val('queryForumForm');
        table.render({
            id: 'forumTableId',
            elem: '#forumTable'
            , url: '/forum/queryByPage'
            , toolbar: '#toolbar'
            , page: true
            , where: _req
            , method: 'get'
            , defaultToolbar: []
            , cols: [
                [
                    {field: 'checkbox', type: 'checkbox'}
                    , {field: 'id', title: 'ID', align: 'center'}
                    , {field: 'title', title: '标题', align: 'center'}
                    , {field: 'content', title: '内容', align: 'center'}
                    , {field: 'userName', title: '楼主名称', align: 'center'}
                    , {field: 'createTime', title: '创建帖子时间', align: 'center'}
                    , {title: '操作', align: 'center', toolbar: '#barDemo'}
                ]
            ]
        });
    }
}
